#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QLabel>
#include<QImage>
#include<QEvent>
#include<QHBoxLayout>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   labelimage1=new QLabel;
   labelimage1->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
   image1.load(":/a.jpg");
   labelimage1->setPixmap(QPixmap::fromImage(image1));
   labelimage2=new QLabel;
   labelimage2->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
   image2.load(":/b.jpg");
   labelimage2->setPixmap(QPixmap::fromImage(image2));
   labelimage3=new QLabel;
   labelimage3->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
   image3.load(":/c.jpg");
   labelimage3->setPixmap(QPixmap::fromImage(image3));

   imagestatus=new QLabel("未选中");
   imagestatus->setAlignment(Qt::AlignHCenter);

   QHBoxLayout*hly=new QHBoxLayout(this);
   hly->addWidget(labelimage1);
     hly->addWidget(labelimage2);
       hly->addWidget(labelimage3);
   QVBoxLayout* vly=new QVBoxLayout(this);
   vly->addLayout(hly);
   vly->addWidget(imagestatus);

   QWidget* wgt=new QWidget(this);
   setCentralWidget(wgt);//控件窗口中间
   wgt->setLayout(vly);

   labelimage1->installEventFilter(this);//安装过滤器
   labelimage2->installEventFilter(this);
   labelimage3->installEventFilter(this);



}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject*watched,QEvent*ev)
{
    if(watched==labelimage1)
     {  if(ev->type()==QEvent::MouseButtonPress)
        {     QMouseEvent*event=(QMouseEvent*)ev;
              if(event->button()==Qt::LeftButton)
              {
                  imagestatus->setText("左键按下，第一张图片");
              }
              else if(event->button()==Qt::RightButton)
              {
                  imagestatus->setText("右键按下，第一张图片");
              }
              else if(event->button()==Qt::MidButton)
              {
                  imagestatus->setText("中键按下，第一张图片");
              }
            QTransform transform;
            transform.scale(2,2);
            QImage temp=image1.transformed(transform,Qt::SmoothTransformation);
           labelimage1->setPixmap(QPixmap::fromImage(temp));
        }
        if(ev->type()==QEvent::MouseButtonRelease)
        {
            QTransform transform;
            transform.scale(1,1);
            QImage tmp=image1.transformed(transform,Qt::SmoothTransformation);
            // Qt::FastTransformation,      // 快速但低质量的变换
            //Qt::SmoothTransformation     // 较慢但高质量的变换
             labelimage1->setPixmap(QPixmap::fromImage(tmp));
        }


     }
    else if(watched==labelimage2)
     {  if(ev->type()==QEvent::MouseButtonPress)
        {     QMouseEvent*event=(QMouseEvent*)ev;
              if(event->button()==Qt::LeftButton)
              {
                  imagestatus->setText("左键按下，第二张图片");
              }
              else if(event->button()==Qt::RightButton)
              {
                  imagestatus->setText("右键按下，第二张图片");
              }
              else if(event->button()==Qt::MidButton)
              {
                  imagestatus->setText("中键按下，第二张图片");
              }
            QTransform transform;
            transform.scale(2,2);
            QImage temp=image2.transformed(transform,Qt::SmoothTransformation);
            labelimage2->setPixmap(QPixmap::fromImage(temp));
        }
        if(ev->type()==QEvent::MouseButtonRelease)
        {
            QTransform transform;
            transform.scale(1,1);
            QImage tmp=image2.transformed(transform,Qt::SmoothTransformation);
            // Qt::FastTransformation,      // 快速但低质量的变换
            //Qt::SmoothTransformation     // 较慢但高质量的变换
             labelimage2->setPixmap(QPixmap::fromImage(tmp));
        }


     }
    else if(watched==labelimage3)
     {  if(ev->type()==QEvent::MouseButtonPress)
        {     QMouseEvent*event=(QMouseEvent*)ev;
              if(event->button()==Qt::LeftButton)
              {
                  imagestatus->setText("左键按下，第三张图片");
              }
              else if(event->button()==Qt::RightButton)
              {
                  imagestatus->setText("右键按下，第三张图片");
              }
              else if(event->button()==Qt::MidButton)
              {
                  imagestatus->setText("中键按下，第三张图片");
              }
            QTransform transform;
            transform.scale(2,2);
            QImage temp=image3.transformed(transform,Qt::SmoothTransformation);
               labelimage3->setPixmap(QPixmap::fromImage(temp));
        }
        if(ev->type()==QEvent::MouseButtonRelease)
        {
            QTransform transform;
            transform.scale(1,1);
            QImage tmp=image3.transformed(transform,Qt::SmoothTransformation);
            // Qt::FastTransformation,      // 快速但低质量的变换
            //Qt::SmoothTransformation     // 较慢但高质量的变换
               labelimage3->setPixmap(QPixmap::fromImage(tmp));
        }


     }





 }

